package com.liser.southernstar.ui.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kunminx.linkage.adapter.viewholder.LinkageSecondaryFooterViewHolder;
import com.kunminx.linkage.adapter.viewholder.LinkageSecondaryHeaderViewHolder;
import com.kunminx.linkage.adapter.viewholder.LinkageSecondaryViewHolder;
import com.kunminx.linkage.bean.BaseGroupedItem;
import com.kunminx.linkage.contract.ILinkageSecondaryAdapterConfig;
import com.liser.southernstar.R;
import com.liser.southernstar.model.vo.GoodsGroupedItem;
import com.liser.southernstar.ui.widget.ElemeAddWidget;

/**
 * 商品主列表适配器
 *
 * @author LISER
 * @since 2020-01-11 16:09
 */
public class GoodsSecondaryAdapter implements ILinkageSecondaryAdapterConfig<GoodsGroupedItem> {

    private static final int SPAN_COUNT = 2;

    private Context context;

    private ElemeAddWidget.OnAddClickListener onAddClickListener;

    public GoodsSecondaryAdapter() {

    }

    @Override
    public void setContext(Context context) {
        this.context = context;
    }

    @Override
    public int getGridLayoutId() {
        return R.layout.default_adapter_linkage_secondary_grid;
    }

    @Override
    public int getLinearLayoutId() {
        return R.layout.adapter_goods_secondary;
    }

    @Override
    public int getHeaderLayoutId() {
        return R.layout.default_adapter_linkage_secondary_header;
    }

    @Override
    public int getFooterLayoutId() {
        return R.layout.adapter_goods_empty_footer;
    }

    @Override
    public int getHeaderTextViewId() {
        return R.id.secondary_header;
    }

    @Override
    public int getSpanCountOfGridMode() {
        return SPAN_COUNT;
    }

    @Override
    public void onBindViewHolder(final LinkageSecondaryViewHolder holder, final BaseGroupedItem<GoodsGroupedItem> item) {
        Glide.with(context).load(item.info.getMain_img()).into((ImageView) holder.getView(R.id.iv_goods_img));
        ((TextView) holder.getView(R.id.iv_goods_name)).setText(item.info.getName());
        ((TextView) holder.getView(R.id.iv_goods_detail)).setText(item.info.getDescribe());
        ((TextView) holder.getView(R.id.iv_goods_price)).setText(item.info.getStrPrice(context));

        ElemeAddWidget elemeAddWidget = holder.getView(R.id.eleme_add_widget);
        if (onAddClickListener != null) {
            elemeAddWidget.setData(onAddClickListener, item.info);
        }
    }

    @Override
    public void onBindHeaderViewHolder(LinkageSecondaryHeaderViewHolder holder, BaseGroupedItem<GoodsGroupedItem> item) {
        TextView tvHeader = holder.getView(R.id.secondary_header);
        tvHeader.setText(item.header);
    }

    @Override
    public void onBindFooterViewHolder(LinkageSecondaryFooterViewHolder holder, BaseGroupedItem<GoodsGroupedItem> item) {

    }

    public GoodsSecondaryAdapter setOnAddClickListener(ElemeAddWidget.OnAddClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
        return this;
    }
}
